package com.rjkj.medicalwaste.adapter

import android.content.Context
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.RelativeSizeSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.rjkj.medicalwaste.bean.GarbageBean
import com.rjkj.medicalwaste.bean.MedicalWasteType
import com.rjkj.medicalwaste.databinding.ItemCollectListBinding

class CollectListAdapter(val context: Context,val itemClick: ItemClick<GarbageBean>) : PagingDataAdapter<GarbageBean, CollectListAdapter.ViewHolder>(COMPARATOR) {
    companion object {
        private val COMPARATOR = object : DiffUtil.ItemCallback<GarbageBean>() {
            override fun areItemsTheSame(oldItem: GarbageBean, newItem: GarbageBean): Boolean {
                return oldItem.garbageBoxQRCode == newItem.garbageBoxQRCode
            }

            override fun areContentsTheSame(oldItem: GarbageBean, newItem: GarbageBean): Boolean {
                return oldItem == newItem
            }
        }
    }

    class ViewHolder(private val binding: ItemCollectListBinding) : RecyclerView.ViewHolder(binding.root) {

        fun bind(garbageBean: GarbageBean, context: Context) {
            garbageBean.garbageTime.split(" ").let {
                if (it.size >= 2) {
                    binding.tvTimeYmd.text = it[0].substring(2, it[0].length)
                    binding.tvTimeHms.text = it[1]
                }
            }
            binding.tvWeight.text = garbageBean.garbageWeight
            MedicalWasteType.getTypeByTypeName(garbageBean.garbageType).let {
                binding.tvType.text = it.alias
                if (it.colorId != 0)
                    binding.tvType.setTextColor(ContextCompat.getColor(context, it.colorId))
            }

        }
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        getItem(position)?.let { garbage->
            holder.bind(garbage, context)
            holder.itemView.setOnClickListener{
                itemClick.onClicked(it,garbage)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemCollectListBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false)
        return ViewHolder(binding)
    }
}